package com.sys.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.sys.bean.Car;
import com.sys.bean.RespBean;
import com.sys.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/admin/carController")
public class CarController{
    @Autowired
    private CarService carService;
    @Value("5")
    private Integer pageSize;

    //新增车辆
    @PostMapping
    public RespBean insert(@RequestBody Car car){
        carService.insert(car);
           return RespBean.ok("添加成功");
    }

    //根据页数查询
    @GetMapping
    public RespBean selectByPage(Integer pageNum, String ownerName) {
        if(pageNum == null) {
            pageNum = 1;
        }
        PageHelper.startPage(pageNum, pageSize);
        List<Car> carList = carService.selectByOwnerName(ownerName);
        PageInfo<Car> pageInfo = new PageInfo<>(carList,5);
        return RespBean.ok("查询成功", pageInfo);
    }

    //删除车辆（软删除）
    @DeleteMapping("/{carId}")
    public RespBean delete(@PathVariable("carId") Integer carId){
        carService.delete(carId);
        return RespBean.ok("出库成功");
    }

    //修改车辆
    @PutMapping
    public RespBean update(@RequestBody Car car){
        carService.update(car);
        return RespBean.ok("修改成功");
    }

    //根据id查询
    @GetMapping("/{carId}")
    public RespBean selectByCarId(@PathVariable Integer carId){
            Car car = carService.selectByCarId(carId);
            return RespBean.ok("查询成功", car);
    }
    //查询所有
    @GetMapping("/selectAll")
    public List<Car> selectAll() {
        List<Car> carList = carService.selectAll();
        return carList;
    }



}
